package com.sc.nft.controller;


import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dto.CollectionChipUserGetDTO;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.CollectionChipUserDetailsVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CollectionChipService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/collection/chip")
@Api(tags = "芯片程序")
public class CollectionChipController {
    private final CollectionChipService collectionChipService;
    private final Redisson redisson;

    @PostMapping(value = "/userGetCollectionChip", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取芯片信息", notes = "获取芯片信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<CollectionChipUserDetailsVO> userGetCollectionChip(@RequestBody CollectionChipUserGetDTO dto) {
        return Result.success(collectionChipService.userGetCollectionChip(dto,UserPool.getUser()));
    }


    @PostMapping(value = "/userGetCollectionChipReceive", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "领取芯片", notes = "领取芯片", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userGetCollectionChipReceive(@RequestBody CollectionChipUserGetDTO dto) {
        RLock lock = redisson.getLock(RedisKeyEnum.CHIP_RECEIVE_COLLECTION.getKey() + dto.getCode());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                collectionChipService.userGetCollectionChipReceive(dto,UserPool.getUser());
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("当前人数过多，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();


    }


    @PostMapping(value = "/userGetCollectionChipRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "识别记录", notes = "识别记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userGetCollectionChipRecord(@RequestBody CollectionChipUserGetDTO dto) {
        collectionChipService.userGetCollectionChipRecord(dto,UserPool.getUser());
        return Result.success();
    }
}
